package cn.edu.ctbu.datajpa01.service.impl;


import cn.edu.ctbu.datajpa01.constain.REnum;
import cn.edu.ctbu.datajpa01.domain.Student;
import cn.edu.ctbu.datajpa01.exception.RException;
import cn.edu.ctbu.datajpa01.repository.StudentRepository;
import cn.edu.ctbu.datajpa01.service.StudentService;
import lombok.Builder;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Builder

@Service
public class StudentImplService implements StudentService {


    private final StudentRepository studentRepository;
    public List<Student> findAll(){
        return studentRepository.findAll();
    }

    /**
     * 只分页的查询
     * @param pageable
     * @return
     */
    public Page<Student> findAll(Pageable pageable){
        return studentRepository.findAll(pageable);
    }
    /*
     * 按id进行查询
     * @param id 主键，整数
     * @return
     * */
    public Student findById(Integer id){


        return studentRepository.findById(id).orElse(null);/*orElse,如果找到了id正常返回，没找到返回空*/
    }

    /**
     * 分页，带过滤的查询
     * @param pageable
     * @return
     */
    public Page<Student> findAll(Example<Student> student,Pageable pageable){
        return studentRepository.findAll(student,pageable);
    }

    /*
     * 按名字进行查询，Like
     * @param id 主键，整数
     * @return
     * */
    public List<Student> findByName(String name){


        return studentRepository.findByNameLike(name);
    }

    /*
     * 按工号查询
     * @param num
     * @return
     * */
    public String[] findByFirstFourNum(){

        String[] firstFourNum = new String[studentRepository.findAll().size()];
        for (int i = 0; i < studentRepository.findAll().size(); i++){
            firstFourNum[i] = String.valueOf(studentRepository.findAll().get(i).getNum()).substring(0,4);
        }
        return firstFourNum;
    }
    public int[] max(){
        int[] max = new int[studentRepository.findAll().size()];
        for (int i = 0; i < studentRepository.findAll().size(); i++){
            max[i] = Integer.parseInt(String.valueOf(findByFirstFourNum()[i]));
        }
        bubbleSort(max);
        return max;
    }

    public static void bubbleSort(int[] arr){
        int n = arr.length;
        for (int i = 0;i < n - 1;i++){
            for (int j = 0;j < n - i - 1;j++){
                if (arr[j] > arr[j + 1]){
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
    public Student insert(Student student){
        studentRepository.save(student);
        return student;
    }

    public Student update(Student student){
        studentRepository.save(student);
        return student;
    }

    public void delete(Student student){
        studentRepository.delete(student);

    }

    public void delete(Integer id){
        Student student = new Student();
        student.setId(id);
        studentRepository.delete(student);
    }

    public Student getById(Integer id) {
        Student student = studentRepository.findById(id).get();
        return student;
    }

    public List<Student> findByNumAndPassword(String num,String password){
        return studentRepository.findByNumAndPassword(num, password);
    }


    @Override
    public Student validateUsernameAndPassword(String num,String password) throws Exception{
        List<Student> students = studentRepository.findByNum(num);
        if (students.size() > 0) {
            //可能对password加密，此处不写
            Student student = students.get(0);
            if (student.getPassword().equals(password)){
                //成功
                return student;


            }else {
                throw new RException(REnum.LOGIN_ERR);
            }



        }else {
            throw new RException(REnum.LOGIN_ERR);
        }
    }


}
